/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "Ucop/Crypto/Sha1.h"
#include <cstdarg>
#include "Ucop/Crypto/BigNumber.h"


namespace Ucop {
namespace Crypto {

	Sha1Hash::Sha1Hash(void)
	{
		SHA1_Init(&_Myctx);
	}

	Sha1Hash::~Sha1Hash(void)
	{
		SHA1_Init(&_Myctx);
	}

	void Sha1Hash::Initialize(void)
	{
		SHA1_Init(&_Myctx);
	}

	void Sha1Hash::Finalize(void)
	{
		SHA1_Final(_Mydigest, &_Myctx);
	}

	void Sha1Hash::UpdateData(const std::uint8_t* _Dat, std::size_t _Len)
	{
		SHA1_Update(&_Myctx, (const void*)_Dat, _Len);
	}

	void Sha1Hash::UpdateData(const std::string& _Str)
	{
		UpdateData((const std::uint8_t*)_Str.c_str(), _Str.length());
	}

	void Sha1Hash::UpdateBigNumbers(BigNumber* _X, ...)
	{
		std::va_list _Arg;
		BigNumber* _Bn;

		va_start(_Arg, _X);
		_Bn = _X;
		while (_Bn)
		{
			UpdateData(_Bn->AsByteArray(), _Bn->GetNumBytes());
			_Bn = va_arg(_Arg, BigNumber*);
		}
		va_end(_Arg);
	}

}}